package com.lyy.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lyy.entity.Video;
import com.lyy.Vo.VideoVo;
import com.lyy.entity.param.VideoInsertParam;
import com.lyy.entity.param.VideoUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.VideoService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/video")
public class VideoController {
    public VideoService videoService;

    @RequestMapping("/select-by-video-id/{video-id}")
    public Result selectByVideoId(@PathVariable("video-id") Integer videoId) {
       Video video = videoService.selectByVideoId(videoId);
        if (ObjectUtil.isEmpty(video)) {
            return Result.failure();
        }
        VideoVo videoVo=new VideoVo();
        BeanUtil.copyProperties(video, videoVo);
        return Result.success(videoVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody VideoInsertParam videoInsertParam){
        Video video=BeanUtil.copyProperties(videoInsertParam,Video.class);
        int result =videoService.insertByVideoId(video);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/update")
    public Result update(@RequestBody VideoUpdateParam videoUpdateParam){
        Video video=BeanUtil.copyProperties(videoUpdateParam,Video.class);
        int result=videoService.updateByVideoId(video);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/list")
    public Result list(){
        List<Video> videos=videoService.selectAllVideo();
        if (ObjectUtil.isEmpty(videos)) {
            return Result.failure();
        }
        List<VideoVo> videoVos = BeanUtil.copyToList(videos, VideoVo.class);
        return Result.success(videoVos);
    }
    @RequestMapping("/delete-by-video-id/{video-id}")
    public Result deleteByVideoId(@PathVariable("video-id") Integer videoId) {
        int result = videoService.deleteByVideoId(videoId);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/delete-by-video-ids")
    public Result deleteByBannerIds(@RequestParam("video-ids") Integer[] videoIds) {
        int result = videoService.deleteByVideoIds(List.of(videoIds));
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/star-count")
    public Result starCount() {
        return Result.success(videoService.starCount());
    }
}
